﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Domain;
using MAF.Common;

namespace WinFormUI
{
    /// <summary>
    /// 新记事（保存和修改）。
    /// </summary>
    public partial class NoteEditView : UserControl
    {
        /// <summary>
        /// 是否新增
        /// </summary>
        private bool _isNew = false;

        /// <summary>
        /// 记事对象。核心对象，本窗体主要就是对它的操作。
        /// </summary>
        private Note _note = null;

        /// <summary>
        /// 新增记事
        /// </summary>
        public NoteEditView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 填充文本框内容
        /// </summary>
        public void FillData(Note note = null)
        {
            if (null == note)
            {
                _isNew = true;
                _note = new Note(CombGuid.NewGuid());
            }
            else
            {
                _isNew = false;
                _note = note;
            }

            txtNew.Text = _note.Content;
        }


        #region 事件
        
        /// <summary>
        /// 保存事件
        /// </summary>
        private void OnSaveButtonClick(object sender, EventArgs e)
        {
            var content = _note.Content = txtNew.Text;
            if (_isNew)
                Notepad.Instance.Memo.AddNote(_note);
            else
                Notepad.Instance.Memo.UpdateNote(_note);

            if (null != SaveButtonClick)
                SaveButtonClick(sender, e);
        }

        /// <summary>
        /// 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDeleteButtonClick(object sender, EventArgs e)
        {
            if (!_isNew)
            {
                Notepad.Instance.Memo.CompleteNote(_note.Id);
            }

            if (null != DeleteButtonClick)
                DeleteButtonClick(sender, e);
        }        

        /// <summary>
        /// 保存按钮点击事件
        /// </summary>
        public event EventHandler SaveButtonClick;

        /// <summary>
        /// 删除按钮点击
        /// </summary>
        public event EventHandler DeleteButtonClick;

        #endregion


    }
}
